import Base from '../utils/base.js';

export default {
  //获取用户信息
    async info(data=null) {
        let params = {
            'api': '/user/info',
        };
        if(data){
            params.data = data
        }
        let res = await Base.request(params)
        if(res && res.status == 'success'){
            return res.data
        }
        return null
    },
	//更新用户信息
    modify(data) {
		let params = {
			'api': '/user/modify',
			'data':data,
			'method':'POST'
		};
        let res = Base.request(params)
        if(res && res.status == 'success'){
            return true
        }
        return false
    },
    //用户登录
    async login(){
       let res = null
       let code = await this._getCode()
       if(code){		
            res = await Base.request({
                data:{
                    code:code
                },
                method:'get',
                api:'/user/token'
            });
            if(res.status == 'success'){
                //保存token到本地
                wx.setStorageSync('token', res.data.token);
            }
        }
        if(res && res.status == 'success'){
            return true
        }
        return false
    },
    //检查token
    async checkToken(){
        let res = await Base.request({
            'api': '/user/checkToken'
        })
        if(res && res.status == 'success'){
            return true
        }
        return false	
    },

    
    //获取登录code
	_getCode(){
		return new Promise((resolve,reject)=>{
            wx.login({
                success: function (res) {
                    let code = 0
                    if(res.errMsg == 'login:ok'){
                        code = res.code
                    }
                    resolve(code)
                }
            });
        })
	}
	
}